package com.xsz.controller;


import com.xsz.entity.User;
import com.xsz.service.UserService;
import com.xsz.util.MD5Utils;
import com.xsz.util.ResultVOUtil;
import com.xsz.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;

@RestController
@RequestMapping("user")
@Api(value = "用户控制类")
public class UserController {
    @Resource
    UserService userService;

    @ApiOperation(value = "登录API")
    @PostMapping("/login")
    public ResultVO selectByNamePwd(@RequestBody User user) {
        System.out.println("username***"+user.getName());
        System.out.println("password***"+user.getPwd());
        String password=user.getPwd();
        String username=user.getName();
        password = MD5Utils.encrypt(username, password);
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(token);
            User loginuser = (User) SecurityUtils.getSubject().getPrincipal();
            return ResultVOUtil.success(loginuser);
        } catch (UnknownAccountException e) {
            return ResultVOUtil.error(500,"账号不存在");
        } catch (IncorrectCredentialsException e) {
            return ResultVOUtil.error(500,"密码错误");
        } catch (LockedAccountException e) {
            return ResultVOUtil.error(500,"账号被锁定");
        } catch (AuthenticationException e) {
            return ResultVOUtil.error(500,"登录失败");
        }
    }


    @ApiOperation(value = "注册API")
    @PostMapping("/register")
    public ResultVO add(@RequestBody  User user) {
        String password = MD5Utils.encrypt(user.getName(), user.getPwd());
        user.setPwd(password);
        return ResultVOUtil.success(userService.add(user));
    }

    @ApiOperation(value = "修改API")
    @PostMapping("/update")
    public ResultVO updateuser(@RequestBody  User user) {
        String password = MD5Utils.encrypt(user.getName(), user.getPwd());
        user.setPwd(password);
        return ResultVOUtil.success(userService.update(user));
    }

    @ApiOperation(value = "注销API")
    @PostMapping("/delete")
    public ResultVO deleteuser(@RequestBody User user) {
        userService.delete(user.getId());
        return ResultVOUtil.success();
    }

    /**查询当前用户信息**/
    @ApiOperation(value = "用户信息")
    @GetMapping("/info")
    public ResultVO showUserInfo(@RequestParam("id") String id){
//        User loginuser = (User) SecurityUtils.getSubject().getPrincipal();

        User userInfo = userService.selectById(id);

        return ResultVOUtil.success(userInfo);
    }

    @ApiIgnore
    @PostMapping("/edit")
    public ResultVO udpate(@RequestBody  User user) {
        if (userService.update(user) == 1) {
            return ResultVOUtil.success();
        } else {
            return ResultVOUtil.error(500, "修改失败");
        }
    }

    @ApiOperation(value = "原密码查询")
    @PostMapping("/showUserByPwd")
    public ResultVO showUserByPwd(@RequestBody  User user) {
        if (userService.selectByIdAndPwd(user) != null) {
            return ResultVOUtil.success();
        } else {
            return ResultVOUtil.error(500, "查询失败");
        }
    }

    @ApiIgnore
    @PostMapping("/pwdEdit")
    public ResultVO pwdEdit(@RequestBody  User user) {
        if (userService.updatePwdById(user) == 1) {
            return ResultVOUtil.success();
        } else {
            return ResultVOUtil.error(500, "修改失败");
        }
    }



//    @RequiresPermissions("userInfo:showAll")//权限管理;
//    @GetMapping("/showAll")
//    public ResultVO selectAll() {
//
//        return ResultVOUtil.success(userService.getAll());
//    }
//
//
//    @RequiresPermissions("userInfo:showByName")//权限管理;
//    @GetMapping("/showByName/{name}")
//    public ResultVO selectByName(@PathVariable("name") String username) {
//
//        return ResultVOUtil.success(userService.likeName(username));
//    }

}
